<?php
/**
 * 教室信息类
 */
namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\model\Equipmentmodel;

use app\admin\model\Strategymodel;
use app\admin\model\Usermodel;
use app\admin\model\Classroom;
use app\admin\model\Classifymodel;
use app\admin\model\Classtype;
use app\admin\model\Classgrade;
use app\admin\validate\Strategy as validate;

class Equipment extends Common
{
    protected $id;
    protected $cfmodel;
    protected $tpmodel;
    protected $grmodel;
    protected $croommodel;
    protected $usermodel;
    protected $equipment;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new Equipmentmodel();
        $this->validateType = 'equipment';
        parent::__construct($app);
        $this->croommodel = new Classroom();
        $this->id = $this->request->param('id', 0);
    }

    public function index()
    {
        $where = $showDataArr = [];

        $dataArr = $this->request->request('search/a', []);

        if($dataArr['title'])  $where[]          = ['title', 'like', "%".$dataArr['title']."%"];
        if($dataArr['type'])   $where[]          = ['type', '=', $dataArr['type']];

        if(session('adminUserInfo.cid')){
            $where[] = ['crid', '=', session('adminUserInfo.cid')];
        }

        $list = $this->localModel->findAllPageInfo('*', $where);
        View::assign(['data'=>$list['data'], 'pageData'=>$list['page'], 'dataArr'=>$dataArr]);
        return view();
    }

    public function add(){
        $showData['crInfoList'] = $this->croommodel->findAllInfo("id, name", [['status', '=', 1]]);


        if($this->id){
            $showData['detail'] = $this->localModel->findDetaiInfoById('*', $this->id);
            View::assign($showData);
            return view('edit');
        }else{
            View::assign($showData);
            return view();
        }
    }

    public function getEquipmentList(){
        $screen_type = $this->request->request('screen_type');
        if(!$screen_type || !session('adminUserInfo.cid'))  return false;

        $equipment = $this->localModel->findAllInfo( 'id, title', [['crid', '=', session('adminUserInfo.cid')], ['type', '=', $screen_type]]);

        if(!$equipment)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'date'=>$equipment]);
    }

}
